import json
import demjson

from crawler.connection import mysql_db
from crawler.logger import monitor_logger

__all__ = ['ElectronicBusinessPipeline']


class ElectronicBusinessPipeline(object):
    def __init__(self):
        pass

    def _insert_data(self, spider, table_name, item):
        """mysql数据插入"""
        try:
            if isinstance(item, dict):
                result = mysql_db.add_smart(table_name, item, update_columns=[k for k, v in item.items()],
                                            insert_ignore=True)
                if result == 1:
                    spider.logger.info('Publish Insert Mysql : {}'.format(demjson.encode(item)))
                elif result == 2:
                    spider.logger.info('Publish Update Mysql : {}'.format(demjson.encode(item)))
                elif result == 0:
                    spider.logger.info('Publish Finish Mysql , Nothing Changes : {}'.format(demjson.encode(item)))
                else:
                    spider.logger.info('Publish Failed Mysql Please Check Over')
            elif isinstance(item, list):
                result = mysql_db.add_batch_smart(table_name, item, update_columns=[k for k, v in item[0].items()])
                if result == 0 or result == 1:
                    for i in item:
                        try:
                            spider.logger.info('Publish Insert Mysql : {}'.format(demjson.encode(dict(i))))
                        except Exception as e:
                            spider.logger.info('Dict Type Error ,Please Ignore Or Check Over : {}'.format(e))
                        spider.logger.info('Publish Insert Mysql : {}'.format(demjson.encode(dict(i))))
                else:
                    spider.logger.info('Publish Failed Mysql Please Check Over')
        except Exception as e:
            spider.logger.info('Publish Failed Mysql : {} | {}'.format(e, demjson.encode(dict(item))))

    def process_item(self, item, spider):
        task = item['origin']
        # 根据不同的任务表名，插入至相应表中
        try:
            table_name = item['table_name']
            self._insert_data(spider, table_name, item['item'])
            # 记录链路日志
            monitor_logger.info({
                **task,
                'service': spider.name,
                'logLevel': 'INFO',
                'crawlerStep': 'insert_data',
                'status': 200,
                'message': '',
                'other': '',
            })
        except Exception as e:
            spider.logger.info('Publish Failed Mysql : {} | {}'.format(e, json.dumps(item['item'])))
            monitor_logger.info({
                **task,
                'service': spider.name,
                'logLevel': 'ERROR',
                'crawlerStep': 'insert_data',
                'status': 500,
                'message': e,
                'other': '',
            })
        return item
